
enum choosetool
{
	move,
	scal,
	choose,
    rota
}
class CObjectEditor {
	moveassist:MOVEASSIST
	scalassist:SCALASSIST
	chooseshap:egret.Shape
	private static myself:CObjectEditor
	stage:egret.Stage
	choosetool1:choosetool=choosetool.choose;
	rotaassist:ROTAASSIST
	private constructor() {
	}
	public static get editor():CObjectEditor
	{
		if(this.myself==undefined)
		{
			this.myself=new CObjectEditor
			this.myself.stage=egret.MainContext.instance.stage;
		}
		return this.myself
	}
	public moveobject()
	{   //if(this.rotaassist!=undefined)
		//this.rotaassist.closerota();
		CMainEditor.editor.isROTA=false;
		this.choosetool1=choosetool.move //修改状态
	
			let b=CMainEditor.editor.CurrentGoalstagexy  //如果是还未添加进主窗口的对象返回错误坐标，0，0
			let stagex=b.x
			let stagey=b.y
			if(stagex==0&&stagey==0)return;

			console.log(b.x,b.y)
		this.chackchange();  //清空当前助手
		if(this.moveassist==undefined)this.moveassist=new MOVEASSIST //第一次创建
		this.stage.addChild(this.moveassist); //添加进舞台
		let a=this.moveassist.offsetxy;       //得到上下两个箭头的自身宽度偏离值
		                                      //计算y的偏移值 底部对齐式
		let offsety=stagey-this.moveassist.height+a.y+CMainEditor.editor.goal.height*CMainEditor.editor.goal.scaleY
		
		this.moveassist.x=stagex-a.x;          
		this.moveassist.y=offsety;
	}
	public scalobject()
	{      
		    CMainEditor.editor.isROTA=false;
		    this.choosetool1=choosetool.scal
			
			let a=CMainEditor.editor.CurrentGoalstagexy  //如果是还未添加进主窗口的对象返回错误坐标，0，0
			let stagex=a.x
			let stagey=a.y
			if(stagex==0&&stagey==0)return;
		
		this.chackchange();
		if(this.scalassist==undefined)this.scalassist=new SCALASSIST
		this.stage.addChild(this.scalassist);
		this.scalassist.resetpos() //调用类方法，修正坐标
	}
	public rotaobject()
	{
            this.choosetool1=choosetool.rota
		    let a=CMainEditor.editor.CurrentGoalstagexy  //如果是还未添加进主窗口的对象返回错误坐标，0，0
			let stagex=a.x
			let stagey=a.y
			if(stagex==0&&stagey==0)return;
			CMainEditor.editor.isROTA=true;
			  this.chackchange();
			  if(this.rotaassist==undefined)this.rotaassist=new ROTAASSIST
		      this.stage.addChild(this.rotaassist);
			  this.rotaassist.setup(stagex,stagey);
	}
	public chackchange()
	{
		 if(this.rotaassist!=undefined)
		    this.rotaassist.closerota();
		if(this.stage.getChildIndex(this.moveassist)!=-1)
		{
			this.stage.removeChild(this.moveassist);
		}
		if(this.stage.getChildIndex(this.scalassist)!=-1)
		{
			this.stage.removeChild(this.scalassist);
		}
		if(this.stage.getChildIndex(this.chooseshap)!=-1)
		{
			this.stage.removeChild(this.chooseshap);
		}
		if(this.stage.getChildIndex(this.rotaassist)!=-1)
		{
			this.stage.removeChild(this.rotaassist);
		}
	}
	public chooseobject()
	{
		if(this.rotaassist!=undefined)
		this.rotaassist.closerota();
		CMainEditor.editor.isROTA=false;
		this.choosetool1=choosetool.choose
		if(this.chooseshap==undefined)this.chooseshap=new egret.Shape
		this.chackchange();
		if(CMainEditor.editor.goal==undefined)return 
		let a=CMainEditor.editor.CurrentGoalstagexy  //如果是还未添加进主窗口的对象返回错误坐标，0，0
			let stagex=a.x
			let stagey=a.y
		if(stagex==0&&stagey==0)return;
        this.drawchooseshape();
		this.stage.addChild(this.chooseshap)
	}
	public update() //更新选项
	{
		if(CCollitionEditor.editor.state)
		{
			this.chackchange();
			return;
		}
		else if(this.choosetool1==choosetool.move)
	      this.moveobject();
		else if(this.choosetool1==choosetool.scal)
		  this.scalobject();
		else if(this.choosetool1==choosetool.choose)
		{
			this.chooseobject();
		}
		else if(this.choosetool1==choosetool.rota)
		{
			this.rotaobject();
		}
	}
	private drawchooseshape()
	{
	  this.chooseshap.graphics.clear();
      let a=CMainEditor.editor.CurrentGoalstagexy
	  let target=CMainEditor.editor.goal
	  a.x-=target.anchorOffsetX*target.scaleX-5
	  a.y-=target.anchorOffsetY*target.scaleY-5
	  let o=target.localToGlobal(target.anchorOffsetX,target.anchorOffsetY);
	  let b=this.Pointrevolve(a.x+target.width*target.scaleX+5,a.y,o,target.rotation)
	  let c=this.Pointrevolve(a.x+target.width*target.scaleX+5,a.y+target.height*target.scaleY+5,o,target.rotation)
	  let d=this.Pointrevolve(a.x,a.y+target.height*target.scaleY+5,o,target.rotation)
	  let e=this.Pointrevolve(a.x,a.y,o,target.rotation)
	  this.chooseshap.graphics.lineStyle(5,0xffff00)
	  this.chooseshap.graphics.lineTo(b.x,b.y)
	  this.chooseshap.graphics.lineTo(c.x,c.y)
	  this.chooseshap.graphics.lineTo(d.x,d.y)
	  this.chooseshap.graphics.lineTo(e.x,e.y)
	  this.chooseshap.graphics.lineTo(b.x,b.y)
	  //this.chooseshap.graphics.endFill();
	}

	public Pointrevolve(x:number,y:number,o:egret.Point,angle:number):egret.Point //一点绕任意一点旋转任意度数
	{
		 
		 var point =new egret.Point
		 point.x=(x-o.x)*Math.cos(angle*Math.PI / 180)-(y-o.y)*Math.sin(angle*Math.PI / 180)+o.x
		 point.y=(x-o.x)*Math.sin(angle*Math.PI/180)+(y-o.y)*Math.cos(angle*Math.PI / 180)+o.y
		
		 return point
	}

}